import { getApiListApi } from '@/api/system/sysapi/api'
import { ApiListParam } from '@/api/system/sysapi/ApiModel'
import { StatusCode } from '@/http/request'
import { nextTick, onMounted, reactive, ref } from 'vue'

export default function useApiTable() {
    const refreshTable = ref(true)
    // 表格树是否展开
    const tableExpandAll = ref(false)

    // 表格高度
    const tableHeight = ref(0)

    // 定义表格数据
    const apiTable = reactive({
        list: []
    })

    // 定义列表查询的参数
    const listParam = reactive<ApiListParam>({
        current_page: 1,
        page_size: 20,
        description: '',
        path: '',
        method: '',
        total: 0
    })

    // 获取角色列表
    const getApiList = async () => {
        let res = await getApiListApi(listParam)
        if (res && res.code == StatusCode.Success) {
            console.log('api列表：', res)
            apiTable.list = res.data.list
            listParam.total = res.data.total
        }
    }

    // 页容量改变时触发
    const sizeChange = (size: number) => {
        listParam.page_size = size
        getApiList()
    }

    // 页数改变时触发
    const currentChange = (page: number) => {
        listParam.current_page = page
        getApiList()
    }

    // 搜索
    const searchBtn = () => {
        getApiList()
    }

    // 重置
    const resetBtn = () => {
        listParam.description = ''
        listParam.path = ''
        listParam.method = ''
        listParam.current_page = 1
        getApiList()
    }

    const tableExpand = () => {
        refreshTable.value = false
        tableExpandAll.value = !tableExpandAll.value
        nextTick(() => {
            refreshTable.value = true
        })
    }

    // 首次加载
    onMounted(() => {
        getApiList()
        nextTick(() => {
            tableHeight.value = window.innerHeight - 210
        })
    })

    return {
        refreshTable,
        tableExpandAll,
        tableHeight,
        apiTable,
        listParam,
        getApiList,
        sizeChange,
        currentChange,
        searchBtn,
        resetBtn,
        tableExpand
    }
}
